﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using AzukiMap.MapSource;

namespace AzukiMapUI
{
	public partial class VirtualEarthProperties : TileSourceProperties
	{
        VirtualEarthSource _tmpSource;
        const string VE_BASE_NAME = "VE tile ";

        public VirtualEarthSource VeSource
        {
            set
            {
                _tmpSource = value;
                SetForm();
            }
        }

        public override MapSource MapDataSource
        {
            get {
                string sourceName = VE_BASE_NAME + FormInputControl.GetSelectedTextFromCombo(CbLayers);
                VirtualEarthSource veSource = new VirtualEarthSource(sourceName);
                SetAll(veSource);
                return veSource;  
            }
        }

		public VirtualEarthProperties()
		{
			// Required to initialize variables
			InitializeComponent();
            _tmpSource = new VirtualEarthSource(VE_BASE_NAME);
            SetForm();
            CheckAllFormData();
		}

        void SetForm()
        {
            if (Content == null)
                return;

            FormInputControl.SetForm(TbServerPaths, _tmpSource.BaseUris);
            FormInputControl.SetForm(TbScales, _tmpSource.Scales);
            FormInputControl.SetForm(TbSrid, _tmpSource.Srid);
            FormInputControl.SetForm(TbTileWidth, _tmpSource.TileWidth);
            FormInputControl.SetForm(TbTileHeight, _tmpSource.TileHeight);
            FormInputControl.SetForm(TbTileDpi, _tmpSource.Dpi);
        }
        
        #region set data from form

        void SetAll(VirtualEarthSource veSource)
        {
            SetServerPathes(veSource);
            SetScales(veSource);
            SetLayer(veSource);
            SetSrid(veSource);
            SetTileWidth(veSource);
            SetTileHeight(veSource);
            SetDpi(veSource);
        }

        void SetServerPathes(VirtualEarthSource veSource)
        {
            if ( CheckServerPaths()) 
                FormInputControl.SetListDataFromForm(veSource.BaseUris, TbServerPaths.Text);
        }

        void SetScales(VirtualEarthSource veSource)
        {
            if (CheckScales())
                FormInputControl.SetListDataFromForm(veSource.Scales, TbScales.Text);
            veSource.Scales.Sort();
        }

        void SetLayer(VirtualEarthSource veSource)
        {
            if ( CbLayers != null )
                veSource.TileType = GetVELayerType(FormInputControl.GetSelectedTextFromCombo(CbLayers));
        }

        void SetSrid(VirtualEarthSource veSource)
        {
            if (CheckSrid())
                veSource.Srid = int.Parse(TbSrid.Text);
        }

        void SetTileWidth(VirtualEarthSource veSource)
        {
            if (CheckTileWidth())
                veSource.TileWidth = int.Parse(TbTileWidth.Text);
        }

        void SetTileHeight(VirtualEarthSource veSource)
        {
            if (CheckTileHeight())
                veSource.TileHeight = int.Parse(TbTileHeight.Text);
        }

        void SetDpi(VirtualEarthSource veSource)
        {
            if (CheckTileDpi())
                veSource.Dpi = int.Parse(TbTileDpi.Text);
        }

        #endregion

        #region check form data

        public override bool CheckAllFormData()
        {
            bool isOk1 = CheckServerPaths();
            bool isOk2 = CheckScales();
            bool isOk3 = CheckTileWidth();
            bool isOk4 = CheckTileHeight();
            bool isOk5 = CheckTileDpi();
            bool isOk6 = CheckSrid();

            return (isOk1 && isOk2 && isOk3 && isOk4 && isOk5 && isOk6);
        }

        bool CheckServerPaths()
        {
            string inputStr = TbServerPaths.Text;
            return FormInputControl.CheckUriList(inputStr, IconErrorServerPaths);
        }

        bool CheckScales()
        {
            string inputStr = TbScales.Text;
            return FormInputControl.CheckDoubleList(inputStr, IconErrorScales);
        }

        bool CheckTileWidth()
        {
            string inputStr = TbTileWidth.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileWidth);
        }

        bool CheckTileHeight()
        {
            string inputStr = TbTileHeight.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileHeight);
        }

        bool CheckTileDpi()
        {
            string inputStr = TbTileDpi.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorTileDpi);
        }

        bool CheckSrid()
        {
            string inputStr = TbSrid.Text;
            return FormInputControl.CheckNaturalNumber(inputStr, IconErrorSrid);
        }

        #endregion

        void TbServerPaths_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckServerPaths();
        }

        void TbScales_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckScales();
        }

        void TbTileWidth_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileWidth();
        }

        void TbTileHeight_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileHeight();
        }

        void TbTileDpi_LostFocus(object sender, RoutedEventArgs e)
        {
            CheckTileDpi();
        }

        void TbSrid_Loaded(object sender, RoutedEventArgs e)
        {
            CheckSrid();
        }

        void CbLayers_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SetLayer(_tmpSource);
            SetForm();
        }

        VirtualEarthLayerType GetVELayerType(string typeStr)
        {
            typeStr = typeStr.ToUpper();
            VirtualEarthLayerType type;
            switch (typeStr)
            {
                case "ROAD":
                    type = VirtualEarthLayerType.ROAD;
                    break;
                case "AERIAL":
                    type = VirtualEarthLayerType.AERIAL;
                    break;
                case "HYBRID":
                    type = VirtualEarthLayerType.HYBRID;
                    break;
                case "TRAFFIC":
                    type = VirtualEarthLayerType.TRAFFIC;
                    break;
                default:
                    type = VirtualEarthLayerType.ROAD;
                    break;
            }
            return type;
        }

    }
}